package org.corey.dev.voxiverse;


import org.corey.dev.voxiverse.chunk.Chunk;
import org.corey.dev.voxiverse.chunk.Save;
import org.lwjgl.util.vector.Vector3f;

public class Player {
	//public static float PX, PY, PZ;
	
	public Vector3f position = null;
	public float yaw = 0.0f;
	public float pitch = 0.0f;
	
	public static float PlayerX;
	public static float PlayerY;
	public static float PlayerZ;
	
	public static float lastX = 0;
	public static float lastY = 0;
	public static float lastZ = 0;
	
	public static int ChunkX;
	public static int ChunkY;
	public static int ChunkZ;
	public static String filename;
	public static String lastFileName;
	
	public static boolean flying = true;
	
	public Player(float PX, float PY, float PZ){
		//PX = -40f;
		//PY = -682f;
		//PZ = -40f;
		
	position = new Vector3f(PX, PY, PZ);
	}
	
	public void yaw(float amount){
		yaw += amount;
	}
	
	public void pitch(float amount){
		pitch += amount;
	}
	
	
	
	public static void Update(){
		//-9.81m/s in the Y direction or dir = down if can move and jumping = some acceleration
		
		
		PlayerX = (int) -(Game.player.position.x)/Chunk.CUBE_LENGTH;
		PlayerY = (int) -(Game.player.position.y)/Chunk.CUBE_LENGTH;
		PlayerZ = (int) -(Game.player.position.z)/Chunk.CUBE_LENGTH;
		
		ChunkX = (int) ((Chunk.CHUNK_SIZE_X*Chunk.CUBE_LENGTH) * ((int) (PlayerX/(Chunk.CHUNK_SIZE_X - 1))));
		ChunkY = (int) ((Chunk.CHUNK_SIZE_Y*Chunk.CUBE_LENGTH) * ((int) (PlayerY/(Chunk.CHUNK_SIZE_Y - 1))));
		ChunkZ = (int) ((Chunk.CHUNK_SIZE_Z*Chunk.CUBE_LENGTH) * ((int) (PlayerZ/(Chunk.CHUNK_SIZE_Z - 1))));
		
	}


	
	public static boolean CanMove(float reqX, float reqY, float reqZ){
		
		
		int newX = (int) (reqX);
		int newY = (int) (reqY);
		int newZ = (int) (reqZ);
		
		filename = ((int) ((Chunk.CHUNK_SIZE_X*Chunk.CUBE_LENGTH) * ((int) (newX/(Chunk.CHUNK_SIZE_X - 1))))) + "-" + ((int) ((Chunk.CHUNK_SIZE_Z*Chunk.CUBE_LENGTH) * ((int) (newZ/(Chunk.CHUNK_SIZE_Z - 1)))));
		int CCX = (int) ((Chunk.CHUNK_SIZE_X*Chunk.CUBE_LENGTH) * ((int) (newX/(Chunk.CHUNK_SIZE_X - 1))));
		int CCZ = (int) ((Chunk.CHUNK_SIZE_Z*Chunk.CUBE_LENGTH) * ((int) (newZ/(Chunk.CHUNK_SIZE_Z - 1))));
		
		
		boolean x = false,y=false,z=false;
		
		
		if (!(newY == PlayerY) | !(newX == PlayerX) | !(newZ == PlayerZ)){
			
			if (!(newY >= (Chunk.CHUNK_SIZE_Y)) && !(newY <= 0)){
				if(Save.CheckNextChunk((int)PlayerX,(int) newY,(int) PlayerZ, CCX, CCZ) == 0){
					y = true;
				} else {
					y = false;
				}
			} else {
				y = false;
			}
			
			if (!(newX <= 0)){
				if (Save.CheckNextChunk((int)newX,(int) PlayerY,(int) PlayerZ, CCX, CCZ) == 0){
					x = true;
				} else {
					x = true;
				}
			} else {
				x = false;
			}
			
			if (!(newZ <= 0)){
				if (Save.CheckNextChunk((int) PlayerX, (int) PlayerY, newZ, CCX, CCZ) == 0){
					z = true;
				} else {
					z = true;
				}
			} else {
				z = false;
			}
		}
		
		
		
		//if (!(reqX==lastX) && !(reqY==lastY) && !(reqZ==lastZ)){
			if (x&&y&&z){
				lastX = reqX;
				lastY = reqY;
				lastZ = reqZ;
				return true;
			}
		//}
		
		//if (!(reqX==lastX) && !(reqY==lastY)){
			if (x&&y){
				lastX = reqX;
				lastY = reqY;
				return true;
			}
	//	}
		
		//if (!(reqX==lastX) && !(reqZ==lastZ)){
			if (x&&z){
				lastX = reqX;
				lastZ = reqZ;
				return true;
			}
	//	}
		
		//if (!(reqY==lastY) && !(reqZ ==lastZ)){
			if (y&&z){
				lastY = reqY;
				lastZ = reqZ;
				return true;
			}
		//}
		
		//if (!(reqX==lastX)){
			if (x){
				lastX = reqX;
				return true;
			}
		//}
		
		//if (!(reqY==lastY)){
			if (y){
				lastY = reqY;
				return true;
			}
		//}
		
		//if (!(reqZ==lastZ)){
			if (z){
				lastZ = reqZ;
				return true;
			}
		//}
		
		
		return false;
	}
	
}
